package com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.stat;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.PowerChangeMonitorConfig;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.IntFunction;

/**
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/28
 */
@Data
public class DispatchPerformanceDetailReq {
    /**
     * 区域id
     */
    @NotNull(message = "区域id不能为空")
    private Integer accountId;
    /**
     * 车辆编码
     */
    private String userCode;

    /**
     * 运维人员id
     */
    private Integer accountUserId;

    /**
     * 开始时间
     */
    @NotNull(message = "开始时间不能为空")
    private LocalDate startTime;
    /**
     * 结束时间
     */
    @NotNull(message = "结束时间不能为空")
    private LocalDate endTime;
    /**
     * 车辆类型
     * 0-电单车 1-自行车
     */
    @NotNull(message = "车辆类型不能为空")
    private Integer machineType;
    /**
     * 运营分区id列表
     */
    private List<Integer> regionIds;
    /**
     * 闲置区间
     * 0-短时 1-中时 2-长时
     */
    private Integer idleRange;
    /**
     * 归属时段
     * 1-00:00~06:00
     * 2-06:00~12:00
     * 3-12:00~18:00
     * 4-18:00~24:00
     */
    private Integer dispatchInterval;
    /**
     * 页码
     */
    @NotNull(message = "页码不能为空")
    private Integer pageNo;
    /**
     * 每页数量
     */
    @NotNull(message = "每页数量不能为空")
    private Integer pageSize;
    /**
     * 审核类型
     * 0-人工 1-系统
     */
    private Integer auditType;

    /**
     * 导出可选字段列表，和返回值的字段名一致<br>
     * 传null默认导出所有字段，传空列表则不导出任何可选字段<br>
     * 可选值: beforeStatus, afterStatus, beforeTag, firstOrderDuration, afterValidOrder, performance, performanceDetail
     */
    private List<String> exportFields;

    @JsonIgnore
    @JSONField(serialize = false)
    private String idleRangeSqlConcat;

    private static final String BETWEEN_TEMPLATE = " BETWEEN %d AND %d";

    public void buildDispatchSqlCondition(IntFunction<PowerChangeMonitorConfig> selectConfigFn) {
        if (Objects.isNull(this.idleRange)) {
            return;
        }
        PowerChangeMonitorConfig monitorConfig = selectConfig(selectConfigFn);
        switch (this.idleRange) {
            case 0:
                this.idleRangeSqlConcat = String.format(BETWEEN_TEMPLATE, monitorConfig.getIdleShortMin(), monitorConfig.getIdleShortMax());
                return;
            case 1:
                this.idleRangeSqlConcat = String.format(BETWEEN_TEMPLATE, monitorConfig.getIdleMiddleMin(), monitorConfig.getIdleMiddleMax());
                return;
            case 2:
                this.idleRangeSqlConcat = String.format(" >= %d", monitorConfig.getIdleLongMin());
                return;
            default:
                throw new BaseException("未匹配到闲置区间设置");
        }
    }

    private PowerChangeMonitorConfig selectConfig(IntFunction<PowerChangeMonitorConfig> selectConfigFn) {
        return Optional.ofNullable(selectConfigFn.apply(this.accountId))
                .orElseGet(() -> new PowerChangeMonitorConfig().putDefaultValue(this.accountId));
    }
}
